const regexPattern = document.getElementById("pattern");
const stringToTest = document.getElementById("test-string");
const testButton = document.getElementById("test-btn");
const testResult = document.getElementById("result");
const caseInsensitiveFlag  = document.getElementById("i");
const globalFlag = document.getElementById("g");

const getFlags = () => {
  return (caseInsensitiveFlag.checked? "i": "") + (globalFlag.checked? "g": "");
}

testButton.addEventListener("click", () => {
  const regex = new RegExp(regexPattern.value, getFlags());
  let str = stringToTest.innerHTML;
  const obj = str.match(regex);
  if (!obj) {
    result.textContent = "no match";
  } else {
    const matchedStr = obj[0];
    const index = obj.index;
    result.textContent = matchedStr;
  }
})
